using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.IO;

namespace My.IdlConverter.Core.Util
{
    public static class PathHelper
    {
        private static StringCollection __subDirs = new StringCollection();

        public static void RecursiveToCreateDirectory(string dir)
        {
            if (Directory.Exists(dir) && __subDirs.Count == 0)
                return;
            else if (Directory.Exists(dir))
            {
                int dirCount = __subDirs.Count;
                if (dirCount > 0)
                {
                    for (int i = 0; i < __subDirs.Count; ++i)
                        Directory.CreateDirectory(__subDirs[dirCount - i - 1]);
                }
                __subDirs.Clear();
            }
            else
            {
                __subDirs.Add(dir);
                string parentDir = Directory.GetParent(dir).FullName;
                RecursiveToCreateDirectory(parentDir);
            }
        }

        public static void CopyFile(string srcFileName, string dstFileName, bool overRide)
        {
            System.Diagnostics.Debug.Assert(srcFileName.Length > 0);
            System.Diagnostics.Debug.Assert(dstFileName.Length > 0);

            if (srcFileName.IndexOf("vssver.scc") != -1)
                return;

            if (!File.Exists(dstFileName))
            {
                string tmpFileName;
                int tmpIndex;
                tmpIndex = dstFileName.LastIndexOf("\\");
                tmpFileName = dstFileName.Substring(tmpIndex);
                PathHelper.RecursiveToCreateDirectory(dstFileName.Remove(tmpIndex, tmpFileName.Length));
                File.Create(dstFileName).Close();
            }
            File.SetAttributes(dstFileName, FileAttributes.Normal);
            File.Copy(srcFileName, dstFileName, overRide);
        }
    }
}
